<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* 
	验证码
*/
class Caption{
	
	// 宽度
	public $width;
	
	// 高度
	public $height;
	
	// 长度
	public $length;
	
	// 类型 1 = 纯数字，2 = 纯字母，3 = 数字字母混合
	public $type;
	
	// 字体
	public $font_path;
	
	// 字体颜色
	public $font_color = array(0, 0, 0);
	
	// 字体大小
	public $font_size;
	
	// 旋转角度
	public $font_angle;
	
	// 图片资源
	private $image;
	
	// 验证码
	private $code;
	
	public function __construct($option = array()){
		$this->width = isset($option['width']) ? $option['width'] : 60;
		$this->height = isset($option['height']) ? $option['height'] : 30;
		$this->length = isset($option['length']) ? $option['length'] : 4;
		$this->type = isset($option['type']) ? $option['type'] : 1;
		$this->font_path = isset($option['font_path']) ? $option['font_path'] : BASEPATH.'fonts/texb.ttf';
		$this->font_size = isset($option['font_size']) ? $option['font_size'] : 20;
		$this->font_angle = isset($option['font_angle']) ? $option['font_angle'] : array(-20, 20);
	}
	
	private function seed_string(){
		switch($this->type){
			case 2:
				$string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
				break;
			case 3:
				$string = '1234567890ABCDEFGHIJKLMNPQRSTUVWXYZ';
				break;
			default:
				$string = '1234567890';
				break;
		}
		return $string;
	}
	
	
	// 计算字符宽、高属性
	private function calculate_textbox($font_size, $font_angle, $font_file, $text) {
		$box = imagettfbbox($font_size, $font_angle, $font_file, $text);
		$min_x = min(array($box[0], $box[2], $box[4], $box[6]));
		$max_x = max(array($box[0], $box[2], $box[4], $box[6]));
		$min_y = min(array($box[1], $box[3], $box[5], $box[7]));
		$max_y = max(array($box[1], $box[3], $box[5], $box[7]));
		return array(
			'left' => ($min_x >= -1) ? -abs($min_x + 1) : abs($min_x + 2),
			'top' => abs($min_y),
			'width' => $max_x - $min_x,
			'height' => $max_y - $min_y,
			'box' => $box
		);
	}
	
	public function rand_code(){
		$seed = $this->seed_string();
		$code = '';
		for($i = 0; $i < $this->length; $i++){
			$code .= $seed[mt_rand(0, strlen($seed) - 1)];
		}
		$this->code = $code;
		return strtolower($code);
	}
	
	public function create(){
		if(!$this->code){
			$this->rand_code();
		}
		$width = $this->width;
		$height = $this->height;
		$this->image = imagecreatetruecolor($width, $height);
		$bgcolor = imagecolorallocate($this->image, 255, 255, 255);
		$ttfcolor = imagecolorallocate($this->image, $this->font_color[0], $this->font_color[1], $this->font_color[2]);
		imagefilledrectangle($this->image, 0, 0, $width, $height, $bgcolor);
		$x = 5;
		for($i = 0; $i < $this->length; $i++){
			$char = $this->code[$i];
			$angle = mt_rand($this->font_angle[0], $this->font_angle[1]);
			$textbox = $this->calculate_textbox($this->font_size, $angle, $this->font_path, $char);
			$y = $textbox['height'] + ($height - $textbox['height']) / 2 - 3;
			imagettftext($this->image, $this->font_size, $angle, $x, $y, $ttfcolor, $this->font_path, $char);
			$x = $x + $textbox['width'] * 0.75 - $textbox['left'];
		}
		header('Content-type: image/png');
		imagepng($this->image);
		imagedestroy($this->image);
	}
	
}